package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jx.model.Admin;
import com.jx.service.AdminService;
import com.jx.servicrimpl.AdminServiceImpl;

import net.sf.json.JSONObject;

/**
 * 修改当前管理员的个人信息
 * @author yu
 *
 */
public class UpdateAdminInFoController extends HttpServlet{
	AdminService adminservice = new AdminServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		 
		//没修改前的admin信息
		HttpSession session =  req.getSession();
		Admin aa = (Admin)session.getAttribute("currentadmin");
		int aid = aa.getAid();
		String aname1 = aa.getAname();
		String apsw1 = aa.getApsw();
		int asex1 =  aa.getAsex();
		int aage1 = aa.getAage();
		//从页面拿到的新的admin的信息
		String aname = req.getParameter("aname");
		String apsw = req.getParameter("apsw");
		String sex = req.getParameter("asex");
		int asex = Integer.parseInt(sex);
		int aage = Integer.parseInt(req.getParameter("aage"));
		
		Admin a = new Admin(aid,aname,apsw,asex,aage);
		boolean flog = adminservice.updateAdminInFo(a);
		session.setAttribute("currentadmininfo", a);
		//拼一个json对象
		JSONObject  admin = new JSONObject();
		JSONObject  state1 = new JSONObject();
		JSONObject  state2 = new JSONObject();
		JSONObject  state3 = new JSONObject();
		String sex1 = new String();
		if(asex==1){
			sex1 = "男";
		}else{
			sex1 = "女";
		}
		admin.put("sex", sex1);
		admin.put("age", aage);
		state1.put("state", 1);
		state2.put("state", 2);
		state3.put("state", 3);
		state2.put("adm", admin);
		
		PrintWriter pw = resp.getWriter();
		 if (aname1.equals(aname)&&apsw.equals(apsw1)&&aage==aage1&&asex==asex1){
			if(flog==true){
				pw.write(state3.toString()); 
				
			}
		}else if(aname1.equals(aname)&&apsw.equals(apsw1)){
			if(flog==true){
				pw.write(state2.toString()); 
				
			}
		}
		else{
			if(flog==true){
				pw.write(state1.toString()); 
			}
		}
	}
}
